package com.xuan.friend.controller.user;


import com.xuan.common.core.constants.HttpConstants;
import com.xuan.common.core.controller.BaseController;
import com.xuan.common.core.domain.R;
import com.xuan.common.core.exception.BusinessException;
import com.xuan.friend.aop.CheckUserStatus;
import com.xuan.friend.domain.user.dto.UserDTO;
import com.xuan.friend.domain.user.dto.UserEditDTO;
import com.xuan.friend.domain.user.vo.UserDetailVo;
import com.xuan.friend.domain.user.vo.UserVo;
import com.xuan.friend.service.user.UserService;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    @PostMapping("/code/login")
    public R<String> userLogin(@RequestBody UserDTO userDTO) {
        if (userDTO == null) {
            throw new BusinessException("参数为空");
        }
        return R.ok(userService.userLogin(userDTO));
    }


    @PostMapping("/sendCode")
    public R<Void> sendCode(@RequestBody UserDTO userDTO) {
        if (userDTO == null || StringUtils.isBlank(userDTO.getPhone())) {
            throw new BusinessException("参数为空");
        }
        return toR(userService.sendCode(userDTO));
    }


    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return toR(userService.logout(token));
    }


    @GetMapping("/info")
    public R<UserVo> getUserInfo(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return R.ok(userService.getUserInfo(token));
    }


    @GetMapping("/detail")
    public R<UserDetailVo> getUserDetail() {
        return R.ok(userService.getUserDetail());
    }


    @CheckUserStatus
    @PutMapping("/edit")
    public R<Void> editUser(@RequestBody UserEditDTO userEditDTO) {
        if (userEditDTO == null) {
            throw new BusinessException("参数为空");
        }
        return toR(userService.editUser(userEditDTO));
    }


    @CheckUserStatus
    @PutMapping("/head-image/update")
    public R<Void> updateHeadImage(@RequestBody UserEditDTO userEditDTO) {
        if (userEditDTO == null || StringUtils.isBlank(userEditDTO.getHeadImage())) {
            throw new BusinessException("参数为空");
        }
        return toR(userService.updateHeadImage(userEditDTO.getHeadImage()));
    }


}
